package com.bjt.wms.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjt.wms.sys.common.BaseUploadController;
import com.bjt.wms.sys.service.*;
import com.bjt.wms.sys.model.Employee;
import com.bjt.wms.sys.model.Permission;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

/**
 * 平板终端数据接口
 *
 * @author WenbinJi
 */
public class ApiController extends BaseUploadController {

    private Log logger = Log.getLog(this.getClass());

    @Inject
    private AccessDetailService accessDetailService;

    @Inject
    private DeliverySheetService deliverySheetService;

    @Inject
    private EmployeeService employeeService;

    @Inject
    private FLowDetailService fLowDetailService;

    @Inject
    private WarehouseService warehouseService;

    @Inject
    private MaterialService materialService;

    @Inject
    private DataDicService dataDicService;

    /**
     * 人员刷卡
     */
    public void employeeAccess() {
        JSONObject jsonObject = JSON.parseObject(getRawData());
        System.out.println(jsonObject.toJSONString());

        int employee_id = jsonObject.getInteger("employee_id");
        int warehouse_id = jsonObject.getInteger("warehouse_id");
        accessDetailService.add(jsonObject);

        Permission permission = employeeService.getEmployeePermission(employee_id);
        if (permission != null) {
            String warehouseIds = permission.getStr("warehouse_ids");
            if (warehouseIds != null && warehouseIds.contains("" + warehouse_id + "")) {
                Kv kv = Kv.create();
                kv.set("employee", Employee.dao.findById(employee_id));
                renderJson(Ret.ok().set("data", kv));
                return;
            }
        }

        renderJson(Ret.fail("msg", "该人员无权限进入仓库"));
    }

    /**
     * 配货单查询
     */
    public void listDeliverySheet() {
        List<Record> records = deliverySheetService.listDeliverySheet();
        renderJson(Ret.ok().set("data", records));
    }

    /**
     * 耗材入库
     */
    public void materialInboud() {
        List<String> paths = getPhotoList(3, "inbound");
        JSONObject jsonObject = JSON.parseObject(get("jsonData"));
        renderJson(fLowDetailService.runInOutboud(jsonObject, paths));
    }

    /**
     * 耗材出库
     */
    public void materialOutboud() {
        List<String> paths = getPhotoList(2, "outbound");
        JSONObject jsonObject = JSON.parseObject(get("jsonData"));
        renderJson(fLowDetailService.runInOutboud(jsonObject, paths));
    }

    /**
     * 获取数据字典
     */
    public void getInitData() {
        renderJson(Ret.ok().set("data", dataDicService.getDataDicList()));
    }
}

